/**
 * Created by yesi on 15/4/15.
 */

function getContent(url,data,async,processFun){
    return httpCallContent(url,data,"get",async,processFun);
}

function postContent(url,data,async,processFun){
    return httpCallContent(url,data,"post",async,processFun);
}

function httpCallContent(url,data,type,async,processFun){
    var rdata = null;
    $.ajax({
        url: url,
        datatype: 'json',
        async : async,
        type: type,
        data: data,
        success: function (receiveData) {
            rdata = processReceiveData(receiveData);
            if(processFun!=null){
                processFun(rdata);
            }
        }
    });
    return rdata;
}

function processReceiveData(receiveData){
    var dataJson = null;
    if(typeof(receiveData) == "object"){
        dataJson = receiveData;
    }else{
        dataJson = JSON.parse(receiveData);
    }
    var flag = dataJson.flag;
    if( flag == 1){
        var msg = dataJson.msg;
        alert(msg);
        return  null;
    }else{
        return  dataJson.data;
    }
}

function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]); return null; //返回参数值
}

function getCommonPageParam(url,param,isSearch,columns,proccessFun){
	var pageParam = {
    	"bFilter": isSearch, //搜索过滤
    	"aLengthMenu" : [[5,10],['5','10']],
    	"bSort":false,//是否支持排序功能  
    	"bServerSide": true, //是否启动服务器端数据导入，即要和sAjaxSource结合使用  
    	"oLanguage": {
        	"oPaginate" : {
            	"sFirst" : "第一页",
            	"sPrevious" : " 上一页 ",
            	"sNext" : " 下一页 ",
            	"sLast" : " 最后一页 "
         	},
         	"sSearch":"标题搜索:",
         	"sEmptyTable":"没有可用的数据",
        	"sInfo": "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
        	"sInfoEmpty": "没有记录",
        	"sLengthMenu": "每页显示 _MENU_条",
        	"sProcessing":"正在加载数据...."
    	},
    	"sAjaxSource": url,
    	"aoColumns": columns,
    	"fnServerData" : function(sSource, aoData, fnCallback) {
        	param.aoData = JSON.stringify(aoData);
        	postContent(sSource,param,true,function(rdata){
        		if(rdata!=null){
        			fnCallback(proccessFun(rdata));
        		}
        	});
    	}
	};
	return pageParam;
}
